package com.advaita.xml;

import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.advaita.license.Customer;

public class CustomerListHandlerTest {

	static final String TEST1_FILE_NAME = "TestCustList1.xml";
	static final int TEST1_CUST_COUNT = 1;
	static final String TEST1_LAST_CUST_NAME = "User One";

	static final String TEST2_FILE_NAME = "TestCustList2.xml";
	static final int TEST2_CUST_COUNT = 5;
	static final String TEST2_LAST_CUST_NAME = "User Five";

	static final String TEMP_FILE_NAME = "TempCustList.xml";

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		File tempFile = new File(TEMP_FILE_NAME);
		tempFile.delete();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		File tempFile = new File(TEMP_FILE_NAME);
		tempFile.delete();
	}

	@Test
	public void testReader() {

		// Test handling of a non existent file
		ArrayList<Customer> custList = CustomerListHandler
				.readCustomerList(TEMP_FILE_NAME);
		if (custList == null) {
			fail("ERROR: Expected an empty customuer list, got null");
		} else if (custList.size() != 0) {
			fail("ERROR: Expected an empty customuer list, got one with ["
					+ custList.size() + "] customers.");
		}

		// Test parsing of existing files
		doReaderTest(TEST1_FILE_NAME, TEST1_CUST_COUNT, TEST1_LAST_CUST_NAME);
		doReaderTest(TEST2_FILE_NAME, TEST2_CUST_COUNT, TEST2_LAST_CUST_NAME);
	}

	private void doReaderTest(String fileName, int custCount,
			String lastCustName) {

		try {
			ArrayList<Customer> custList = CustomerListHandler
					.readCustomerList(fileName);

			if (custList == null) {
				fail("No customer list data returned" + " for file[" + fileName
						+ "].");
			}

			if (custList.size() != custCount) {
				fail("Wrong number of customers returned, expected["
						+ custCount + "] got [" + custList.size()
						+ "] for file[" + fileName + "].");
			}

			Customer cust = custList.get(custCount - 1);
			if (!cust.getUser().equals(lastCustName)) {
				fail("Wrong customer name found, expected [" + lastCustName
						+ "] got [" + cust.getUser() + "] for file[" + fileName
						+ "].");
			}

		} catch (Exception e) {
			fail("Got exception:" + e.getMessage() + " for file[" + fileName
					+ "].");
		}
	}

	@Test
	public void testWriter() {
		doWriterTest(TEST1_FILE_NAME, TEST1_CUST_COUNT, TEST1_LAST_CUST_NAME);
		doWriterTest(TEST2_FILE_NAME, TEST2_CUST_COUNT, TEST2_LAST_CUST_NAME);
	}

	private void doWriterTest(String fileName, int custCount,
			String lastCustName) {

		try {
			ArrayList<Customer> custList1 = CustomerListHandler
					.readCustomerList(fileName);
			if (custList1 == null) {
				fail("No customer list data returned" + " for file[" + fileName
						+ "].");
			}
			CustomerListHandler.writeCustomerList(custList1, TEMP_FILE_NAME);
			ArrayList<Customer> custList2 = CustomerListHandler
					.readCustomerList(TEMP_FILE_NAME);

			if (custList1.size() != custList2.size()) {
				fail("Customer lists don't match" + " for file[" + fileName
						+ "].");
			}

			if (custList1.size() != custCount) {
				fail("Wrong number of customers returned, expected 1 got "
						+ custList1.size() + " for file[" + fileName + "].");
			}

			File tempFile = new File(TEMP_FILE_NAME);
			tempFile.delete();

		} catch (Exception e) {
			fail("Got exception:" + e.getMessage() + " for file[" + fileName
					+ "].");
		}
	}

}
